<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>酒店相册管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">

</head>
<body>

<style>
    tr td .layui-table-cell{
        height:130px;
        line-height: 130px;
    }
    .layui-table img{
        max-width: 500px;
    }

</style>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">酒店房间相册列表</div>
                <div class="layui-card-body">

                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <div class="layui-btn-group test-table-operate-btn">
                                <button class="layui-btn" data-type="addadmin">上传图片</button>
                                <button class="layui-btn" onclick="javascript:location.href='javascript:history.go(-1)'">
                                     返回
                                </button>
                            </div>
                        </div>

                    </div>

                    <input name="" type="hidden" value="{$data.room_id}" id="object_id" >
                    <table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate"></table>

                    <script type="text/html" id="test-table-operate-barDemo">
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<!--添加弹窗-->
<div id="addmodal" style="display: none;padding:20px;">


    <div class="layui-upload">
        <button type="button" class="layui-btn layui-btn-normal" id="test-upload-testList">选择多文件</button>
        <div class="layui-upload-list">
            <table class="layui-table">
                <thead>
                <tr><th>文件名</th>
                    <th>大小</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr></thead>
                <tbody id="test-upload-demoList"></tbody>
            </table>
        </div>
        <button type="button" class="layui-btn" id="test-upload-testListAction">开始上传</button>
    </div>

</div>

<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    var room_id=document.getElementById('object_id').value;
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','form','table','laydate','set','upload'], function(){
        $ = layui.jquery;
        var table = layui.table
            ,admin = layui.admin
            ,element = layui.element
            ,layer = layui.layer
            ,laydate = layui.laydate
            ,upload = layui.upload
            ,form = layui.form;

        table.render({
            elem: '#test-table-operate'
            ,url: "{:url('Album/roomImages')}?room_id="+room_id
            ,height: 'full-155'
            ,cols: [[
                 {field:'id',  title: 'id',width:100,align:'center'}
                ,{field:'img_path',align:'center',title: '图片',width:230,style:"height:150px;",templet:function(d){
                        return "<img src='" +d.img_path+ "' style='display:block;width:205px;height:123px;'>"
                    }}
              /*  ,{field:'sort',  title: '排序（点击修改）',edit:'text'}*/
                ,{field:'create_time',  title: '创建时间',align:'center'}
                ,{field:'h_admin_name',  title: '创建人',align:'center'}
                ,{align:'center',width:150, title: '操作', toolbar: '#test-table-operate-barDemo'}
            ]]
            ,page:true
            ,limit:20
        });

        //监听工具条
        table.on('tool(test-table-operate)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('确认删除该图片吗', function(index){
                    var loading = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    $.ajax({
                        url: "{:url('Album/delRoomImg')}",
                        type: 'post',
                        dataType: 'json',
                        data: {id:data.id,path:data.img_path},
                        success: function(msg){
                            layer.close(loading);
                            if (msg.code === 0) {
                                layer.msg('删除成功',{icon:2,time:1000},function(){
                                    obj.del();
                                    layer.close(index);
                                });

                            }else if (msg.code === 1){
                                layer.msg('删除失败',{icon:2,time:1000});
                            }
                        }
                    })
                });
            }
        });

        //监听单元格编辑
        table.on('edit(test-table-operate)', function(obj) {
            var reg = /^[1-9]+[0-9]*]*$/;

            if (!reg.test(obj.value)){
                layer.msg('请输入正整数',{time:1000},function(){
                    table.reload('test-table-operate');
                });
                return false;
            }

            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });

            $.ajax({
                url: "{:url('Album/editRoomSort')}",
                type: 'post',
                dataType: 'text',
                data: {id:obj.data.id,sort:obj.value},
                success: function(msg){
                    layer.closeAll();
                    layer.msg('修改成功',{time:1000},function(){
                        table.reload('test-table-operate');
                    });
                }
            })

        });

        //多文件列表示例
        var demoListView = $('#test-upload-demoList')
            ,uploadListIns = upload.render({
            elem: '#test-upload-testList'
            ,url: "{:url('Album/uploadsRoomImg')}?room_id="+room_id
            ,accept: 'file'
            ,multiple: true
            ,auto: false
            ,bindAction: '#test-upload-testListAction'
            ,choose: function(obj){
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                //读取本地文件
                obj.preview(function(index, file, result){
                    var tr = $(['<tr id="upload-'+ index +'">'
                        ,'<td>'+ file.name +'</td>'
                        ,'<td>'+ (file.size/1014).toFixed(1) +'kb</td>'
                        ,'<td>等待上传</td>'
                        ,'<td>'
                        ,'<button class="layui-btn layui-btn-mini test-upload-demo-reload layui-hide">重传</button>'
                        ,'<button class="layui-btn layui-btn-mini layui-btn-danger test-upload-demo-delete">删除</button>'
                        ,'</td>'
                        ,'</tr>'].join(''));

                    //单个重传
                    tr.find('.test-upload-demo-reload').on('click', function(){
                        obj.upload(index, file);
                    });

                    //删除
                    tr.find('.test-upload-demo-delete').on('click', function(){
                        delete files[index]; //删除对应的文件
                        tr.remove();
                        uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });

                    demoListView.append(tr);
                });
            }
            ,done: function(res, index, upload){
                if(res.code == 0){ //上传成功
                    var tr = demoListView.find('tr#upload-'+ index)
                        ,tds = tr.children();
                    tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                    tds.eq(3).html(''); //清空操作
                    return delete this.files[index]; //删除文件队列已经上传成功的文件
                }
                this.error(index, upload);
            }
            ,error: function(index, upload){
                var tr = demoListView.find('tr#upload-'+ index)
                    ,tds = tr.children();
                tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                tds.eq(3).find('.test-upload-demo-reload').removeClass('layui-hide'); //显示重传
            }
        });

        var $ = layui.$, active = {
            addadmin: function() {
                var addmodal = layer.open({
                    title: '图片上传'
                    , type: 1
                    , shadeClose: false
                    , area: ['700px','800px']
                    , content: $('#addmodal')
                    ,cancel:function(){
                        window.location.reload();
                    }

                });
            }

        };

        $('.test-table-operate-btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


    });

</script>


</body>
</html>